/*
 * @Author: 何 海深 813936564@qq.com
 * @Date: 2023-12-01 21:23:52
 * @LastEditors: 何 海深 813936564@qq.com
 * @LastEditTime: 2023-12-01 21:25:53
 * @FilePath: \gq-one-map-fornt-end\src\utils\webSocket\meetingSocket.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import emitter from '@/utils/mitt/mittIndex'
import Socket from '@/utils/webSocket'
/**
 * @description: 启动融合通信消息推送消息传入参数类型
 * @param url:hostName, confId:会议id
 */
export type ContactInfoType = {
  url?: string // url,ws://172.25.242.178
  confId: string // 会议id
  resSend?: boolean // 是否进行
}
/* url:wss://172.25.242.178/ws/conf/123456 */
/* 提供出来的的url: 'wss://' + window.location.hostname + ':8050/ws/conf/' + conf_id */
/*
 需要对ng进行配置代理
 wss://172.25.242.178/ws/conf/
 代理到：
 wss://yzgivfdm1.panyu.gd.cn:8050/ws/conf/
*/
// const hostName = `wss://172.25.242.178`;//IP地址
const hostName = `wss://czc.panyu.gd.cn` // 域名
/**
 * @description: 拼凑正确的url
 * @param urlInfo -ContactInfoType
 * @param url-hostName,url上的host
 * @param confId-会议id
 * @return url: string;
 */
const getUrl = (urlInfo: ContactInfoType): Promise<string> => {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  return new Promise((resolve, _reject: any) => {
    let url = ''
    url = `${urlInfo.url === '' ? hostName : urlInfo.url}/ws/conf/${urlInfo.confId}`
    resolve(url)
  })
}
/**
 * @description: 融合通信会议状态推送
 * @param {ContactInfoType} obj
 * @return {*}
 */
const beginMeetingWebSocket = async (obj: ContactInfoType) => {
  const url: string = await getUrl(obj)
  const meetingSocket = new Socket({ url: url as string })
  meetingSocket.onMessage((resDatas: any) => {
    // console.log("socket信息", resDatas);
    emitter.emit('MeetingGetMessage', resDatas)
  })
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  emitter.on('closeMeetingSocket', (_resDatas: any) => meetingSocket.destroy())
}
export default beginMeetingWebSocket
